<?php namespace App\Http\Controllers\Admin;

/**
 * 用户账号管理操作相关
 */
use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\Admin_operate;
use App\Models\Coupon;
use App\Models\Customer;
use App\Models\Customer_day;
use App\Models\Customer_month;
use App\Models\Employee;
use App\Models\Job;
use App\Models\Evaluation;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;
//use Illuminate\Support\Facades\Request;

use Illuminate\Http\Request;

class AdminController extends Controller {



    public function index(){
        if(Session::get('login')!=null){
            return view('admin/index');
        }else{
            return view('login/login');
        }
    }

    public function employee(){
        $employees=Employee::all();

        return View('admin/employeeManage')->with('employees',$employees);
    }

    public function testEmployee(){
        $employees=Employee::all();
        return $employees;
    }

    public function addEmployee(){
        $jobs=Job::all();
        return view('admin/addEmployee')->with('jobs',$jobs);
    }

    public function saveEmployee(Request $request){

        $employee=new Employee;
        $employee->name=$request->input('name');
        $employee->gender=$request->input('gender');
        $employee->id_card=$request->input('id_card');
        $employee->age=date("Y")-substr($request->input('id_card'),6,4);
        $employee->imgUrl=$request->input('imgUrl');
        $employee->job=$request->input('job');
        $employee->job_number=$request->input('job_number');
        $employee->address=$request->input('address');
        $employee->evaluation=$request->input('evaluation');
        $employee->description=$request->input('description');

        $employee->save();


        $admin_operate=new Admin_operate;
        $admin_operate->adminId=Session::get('login')->id;
        $admin_operate->adminName=Session::get('login')->name;
        $admin_operate->content='新增了员工: '.$request->input('name');

        $admin_operate->save();

        //$input = Request::all();
        //我们直接返回$input，来看一下
        return redirect()->action('admin\AdminController@employee');

    }

    public function deleteEmployee(){
        $id=$_GET["id"];
        //console.log($id);
        //print_r($id);
        $employee = Employee::find($id);


        $admin_operate=new Admin_operate;
        $admin_operate->adminId=Session::get('login')->id;
        $admin_operate->adminName=Session::get('login')->name;
        $admin_operate->content='删除了员工: '.$employee->name;

        $admin_operate->save();

        $employee->delete();
        return redirect()->action('admin\AdminController@employee');
    }
    public function updateEmployee(){
        $id=$_GET["id"];
        $employee = Employee::find($id);

        $jobs=Job::all();

        return View('admin/updateEmployee')->with('employee',$employee)->with('jobs',$jobs);

    }

    public function doUpdateEmployee(Request $request){

        $employee=Employee::find($request->input('id'));

        $employee->name=$request->input('name');
        $employee->gender=$request->input('gender');
        $employee->id_card=$request->input('id_card');
        $employee->age=date("Y")-substr($request->input('id_card'),6,4);
        $employee->imgUrl=$request->input('imgUrl');
        $employee->job=$request->input('job');
        $employee->job_number=$request->input('job_number');
        $employee->address=$request->input('address');
        $employee->evaluation=$request->input('evaluation');
        $employee->description=$request->input('description');
        $employee->save();


        $admin_operate=new Admin_operate;
        $admin_operate->adminId=Session::get('login')->id;
        $admin_operate->adminName=Session::get('login')->name;
        $admin_operate->content='更新了员工: '.$request->input('name');

        $admin_operate->save();
        //$input = Request::all();
        //我们直接返回$input，来看一下
        return redirect()->action('admin\AdminController@employee');
    }




    public function customer(){
        $customers=Customer::all();

        return View('admin/customerManage')->with('customers',$customers);
    }

    public function addCustomer(){
        $coupons=Coupon::all();
        return view('admin/addCustomer')->with('coupons',$coupons);
    }

    public function saveCustomer(Request $request){

        $customer=new Customer();
        $customer->name=$request->input('name');
        $customer->phoneNumber=$request->input('phoneNumber');
        $customer->coupon=$request->input('coupon');


        $customer->save();

        $admin_operate=new Admin_operate;
        $admin_operate->adminId=Session::get('login')->id;
        $admin_operate->adminName=Session::get('login')->name;
        $admin_operate->content='新增了顾客: '.$request->input('name');

        $admin_operate->save();


        $day=date("Y-m-d");
        $customer_day=Customer_day::where(['day_num'=>$day])->first();
        if($customer_day!=null){
            $customer_day->num=$customer_day->num+1;
        }else{
            $customer_day=new Customer_day();
            $customer_day->day_num=$day;
            $customer_day->num=1;
        }
        $customer_day->save();



        $month=date("Y-m");
        $customer_month=Customer_month::where(['month_num'=>$month])->first();
        if($customer_month!=null){
            $customer_month->num=$customer_month->num+1;
        }else{
            $customer_month=new Customer_month();
            $customer_month->month_num=$month;
            $customer_month->num=1;
        }
        $customer_month->save();

        //$input = Request::all();
        //我们直接返回$input，来看一下
        return redirect()->action('admin\AdminController@customer');

    }

    public function deleteCustomer(){
        $id=$_GET["id"];
        $customer = Customer::find($id);

        $admin_operate=new Admin_operate;
        $admin_operate->adminId=Session::get('login')->id;
        $admin_operate->adminName=Session::get('login')->name;
        $admin_operate->content='删除了顾客: '.$customer->name;

        $admin_operate->save();

        $customer->delete();
        return redirect()->action('admin\AdminController@customer');
    }
    public function updateCustomer(){
        $id=$_GET["id"];
        $customer = Customer::find($id);

        $coupons=Coupon::all();

        return View('admin/updateCustomer')->with('customer',$customer)->with('coupons',$coupons);

    }

    public function doUpdateCustomer(Request $request){

        $customer=Customer::find($request->input('id'));

        $customer->name=$request->input('name');
        $customer->phoneNumber=$request->input('phoneNumber');
        $customer->coupon=$request->input('coupon');

        $customer->save();

        $admin_operate=new Admin_operate;
        $admin_operate->adminId=Session::get('login')->id;
        $admin_operate->adminName=Session::get('login')->name;
        $admin_operate->content='更新了顾客: '.$request->input('name');

        $admin_operate->save();
        //$input = Request::all();
        //我们直接返回$input，来看一下
        return redirect()->action('admin\AdminController@customer');
    }

    public function outCheckIn(){
        $id=$_GET["id"];
        $customer = Customer::find($id);
        $customer->check_in='已退房';
        $customer->save();
        return redirect()->action('admin\AdminController@customer');
    }


    public function customerStatistics(){
        //$customers=Customer::all();

        //print_r($month_x);

        return View('admin/customerStatistics');
    }
     public function getDay(){
         $limit_day=30;
         $customer_day=Customer_day::where([])->orderBy('created_at','desc')->take($limit_day)->get();

         //array_reverse($customer_day);
         $day_x=array();
         foreach($customer_day as $day){
             array_push($day_x,substr($day->day_num,5));
         }
         $day_x_reve=array_reverse($day_x);
         $day_y=array();
         foreach($customer_day as $day){
             array_push($day_y,$day->num);
         }
         $day_y_reve=array_reverse($day_y);

         $day=array();
         array_push($day,$day_x_reve,$day_y_reve);
         return $day;
     }

    public function getMonth(){
        $limit_month=12;
        $customer_month=Customer_month::where([])->orderBy('created_at','desc')->take($limit_month)->get();

        $month_x=array();
        foreach($customer_month as $month){
            array_push($month_x,$month->month_num);
        }
        $month_x_reve=array_reverse($month_x);
        $month_y=array();
        foreach($customer_month as $month){
            array_push($month_y,$month->num);
        }
        $month_y_reve=array_reverse($month_y);
        $month=array();
        array_push($month,$month_x_reve,$month_y_reve);
        return $month;
    }




    public function admin(){
        $admins=Admin::all();

        return View('admin/adminManage')->with('admins',$admins);
    }

    public function addAdmin(){

        return view('admin/addAdmin');
    }

    public function saveAdmin(Request $request){

        $admin=new Admin;
        $admin->name=$request->input('name');
        $admin->pass=$request->input('pass');

        $admin_operate=new Admin_operate;
        $admin_operate->adminId=Session::get('login')->id;
        $admin_operate->adminName=Session::get('login')->name;
        $admin_operate->content='新增了管理员: '.$request->input('name');

        $admin_operate->save();

        $admin->save();
        //$input = Request::all();
        //我们直接返回$input，来看一下
        return redirect()->action('admin\AdminController@admin');

    }

    public function deleteAdmin(){
        $id=$_GET["id"];
        //console.log($id);
        //print_r($id);
        $admin = Admin::find($id);

        $admin_operate=new Admin_operate;
        $admin_operate->adminId=Session::get('login')->id;
        $admin_operate->adminName=Session::get('login')->name;
        $admin_operate->content='删除了管理员: '.$admin->name;

        $admin_operate->save();

        $admin->delete();
        return redirect()->action('admin\AdminController@admin');
    }
    public function updateAdmin(){
        $id=$_GET["id"];
        $admin = Admin::find($id);

        return View('admin/updateAdmin')->with('admin',$admin);

    }

    public function doUpdateAdmin(Request $request){

        $admin=Admin::find($request->input('id'));

        $admin->name=$request->input('name');
        $admin->pass=$request->input('pass');

        $admin->save();

        $admin_operate=new Admin_operate;
        $admin_operate->adminId=Session::get('login')->id;
        $admin_operate->adminName=Session::get('login')->name;
        $admin_operate->content='更新了管理员: '.$request->input('name');

        $admin_operate->save();
        //$input = Request::all();
        //我们直接返回$input，来看一下
        return redirect()->action('admin\AdminController@admin');
    }

    public function updateAdminLimit(){
        $id=$_GET["id"];
        //console.log($id);
        //print_r($id);
        $admin = Admin::find($id);

        $admin->type=1;
        $admin->save();
        return redirect()->action('admin\AdminController@admin');
    }



    public function job(){
        $jobs=Job::all();

        return View('admin/jobManage')->with('jobs',$jobs);
    }

    public function addJob(){

        return view('admin/addJob');
    }

    public function saveJob(Request $request){

        $job=new Job;
        $job->name=$request->input('name');


        $job->save();

        $admin_operate=new Admin_operate;
        $admin_operate->adminId=Session::get('login')->id;
        $admin_operate->adminName=Session::get('login')->name;
        $admin_operate->content='新增了岗位: '.$request->input('name');

        $admin_operate->save();
        //$input = Request::all();
        //我们直接返回$input，来看一下
        return redirect()->action('admin\AdminController@job');

    }

    /*public function deleteJob(){
        $id=$_GET["id"];
        //console.log($id);
        //print_r($id);
        $employee = Employee::find($id);

        $employee->delete();
        return redirect()->action('admin\AdminController@employee');
    }*/
    public function updateJob(){
        $id=$_GET["id"];
        $job = Job::find($id);

        return View('admin/updateJob')->with('job',$job);

    }

    public function doUpdateJob(Request $request){

        $job=Job::find($request->input('id'));

        $job->name=$request->input('name');

        $job->save();

        $admin_operate=new Admin_operate;
        $admin_operate->adminId=Session::get('login')->id;
        $admin_operate->adminName=Session::get('login')->name;
        $admin_operate->content='更新了岗位: '.$request->input('name');

        $admin_operate->save();
        //$input = Request::all();
        //我们直接返回$input，来看一下
        return redirect()->action('admin\AdminController@job');
    }



    public function coupon(){
        $coupons=Coupon::all();

        return View('admin/couponManage')->with('coupons',$coupons);
    }

    public function addCoupon(){

        return view('admin/addCoupon');
    }

    public function saveCoupon(Request $request){

        $coupon=new Coupon;
        $coupon->name=$request->input('name');


        $coupon->save();

        $admin_operate=new Admin_operate;
        $admin_operate->adminId=Session::get('login')->id;
        $admin_operate->adminName=Session::get('login')->name;
        $admin_operate->content='新增了优惠券: '.$request->input('name');

        $admin_operate->save();
        //$input = Request::all();
        //我们直接返回$input，来看一下
        return redirect()->action('admin\AdminController@coupon');

    }

    /*public function deleteJob(){
        $id=$_GET["id"];
        //console.log($id);
        //print_r($id);
        $employee = Employee::find($id);

        $employee->delete();
        return redirect()->action('admin\AdminController@employee');
    }*/
    public function updateCoupon(){
        $id=$_GET["id"];
        $coupon = Coupon::find($id);

        return View('admin/updateCoupon')->with('coupon',$coupon);

    }

    public function doUpdateCoupon(Request $request){

        $coupon=Coupon::find($request->input('id'));

        $coupon->name=$request->input('name');

        $coupon->save();

        $admin_operate=new Admin_operate;
        $admin_operate->adminId=Session::get('login')->id;
        $admin_operate->adminName=Session::get('login')->name;
        $admin_operate->content='更新了优惠券: '.$request->input('name');

        $admin_operate->save();
        //$input = Request::all();
        //我们直接返回$input，来看一下
        return redirect()->action('admin\AdminController@coupon');
    }

    public function adminOperate(){
        $operates=Admin_operate::all();

        return View('admin/adminOperateManage')->with('operates',$operates);
    }
    public function evaluation(){
        $evaluations = DB::table('evaluation')         //将两张表拼接起来
        ->join('employee', function($join)
        {
            $join->on('evaluation.employeeId', '=', 'employee.id');
        })
            ->join('customer', function($join)
            {
                $join->on('evaluation.customerId', '=', 'customer.id');
            })->select('customer.name as customerName','employee.name as employeeName','evaluation.content')
            ->get();

        return view('admin/evaluationManage')->with('evaluations',$evaluations);
    }
}